﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sec;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.UICommon;
using ESOL.Business.Bpt;
using ESOL.WinUI.Sales;
using ESOL.WinUI.Hr;
using ESOL.Common.Schema;

namespace ESOL.WinUI.Bpt
{
    public partial class frmManageProjectApplication : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private BptApplicationEntity mSelectdeApplication;
        private BptApplicationItemEntity mSelectdeApplicationItem;
        #endregion

        #region Constructor
        public frmManageProjectApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions

        private void BingCombobox()
        {
            UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "DRAW", true);
            UICommonFunctions.BindComboboxByCommonItem(cmbType, "DT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "DRAW", true);
            cmbStatus.SelectedIndex = 1;
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtProApplicationCode.Text) == false)
            {
                condition += " AND B.APPLICATION_CODE LIKE '%" + txtProApplicationCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtApplicationPerson.Text) == false)
            {
                condition += " AND C.NAME_CN LIKE '%" + txtApplicationPerson.Text + "%'";
            }
            if (dtpApplicationDateFrom.EditValue!=null)
            {
                condition += " AND A.CREATED_DATE >'" + ((DateTime)dtpApplicationDateFrom.EditValue).ToString("yyyy-MM-dd") + "'";
            }
            if (dtpApplicationDateTo.EditValue != null)
            {
                condition += " AND A.CREATED_DATE <'" + ((DateTime)dtpApplicationDateTo.EditValue).AddDays(1).ToString("yyyy-MM-dd") + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbType.Text) == false && cmbType.SelectedValue != null)
            {
                condition += " AND A.APPLICATION_TYPE = '" + cmbType.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "APPLICATION_CODE";

            DataTable dt = BptApplicationBs.GetCustomerByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
            gvDetail.DataSource = null;
        }

        private void BingDetailGrid(string ApplictionID)
        {
            if (!ApplictionID.Equals(""))
            {
                DataTable dt = new DataTable();
               dt= BptApplicationItemBs.GetApplicationItemByID(ApplictionID);
               gvDetail.DataSource = dt;
            }
        }

        private void SendMessage(string sMessageTitle, string sSender, string sMessageBody, string MessageLevel)
        {
            MstMessageEntity message = new MstMessageEntity();
            message.MessageId = Guid.NewGuid().ToString();
            message.MessateTitle = sMessageTitle;
            message.MessageSender = sSender;
            message.MessageBody = sMessageBody;
            message.MessageLevel = MessageLevel;
            message.FormName = "ESOL.WinUI.Bpt.frmDrawBlueprintManage";
            //MessageLevel.ML03.ToString();
            message.MessageDate = DateTime.Now;
            List<MstPersonEntity> toList = MstMessageBs.GetPersonIdByFormAndObject("frmDrawBlueprintManage", "btnModify",sSender);
            MstMessageBs.SendMessage(message, toList);
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            dtpApplicationDateFrom.EditValue = DateTime.Now.AddDays(-15);
            BingCombobox();
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectdeApplication = new BptApplicationEntity(dr);
            BingDetailGrid(mSelectdeApplication.ApplicationId);
            if (mSelectdeApplication.Status == "DRAW01")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            if (mSelectdeApplication.Status == "DRAW02")
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = true;
                btnApprove.Enabled = false;
            }
            if (mSelectdeApplication.Status == "DRAW03")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = false;
            }
            if (mSelectdeApplication.Status == "DRAW04")
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnApprove.Enabled = false;
            }
            if (mSelectdeApplication.Status == "DRAW05")
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnApprove.Enabled = false;
            }
            if (mSelectdeApplication.Status == "DRAW06")
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnApprove.Enabled = false;
            }
        }

        protected override void Click_View()
        {
            if (mSelectdeApplication == null)
            {
                MessageBox.Show("请选择一个制图申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProjectApplication frm = new frmMaintainProjectApplication();
            frm.ThisActionType = ActionType.View;
            frm.SelectedBptApplication = mSelectdeApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectdeApplication == null)
            {
                MessageBox.Show("请选择一个制图申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProjectApplication frm = new frmMaintainProjectApplication();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedBptApplication = mSelectdeApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {

            frmMaintainProjectApplication frm = new frmMaintainProjectApplication();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectdeApplication == null)
            {
                MessageBox.Show("请选择一个制图申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!mSelectdeApplication.ApprovedBy.Equals("") )
            {
                if (mSelectdeApplication.ApprovedBy != CommonParameter.CurrentUser.PersonId)
                {
                    MessageBox.Show("您未被指定为该申请的审核人！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            frmMaintainProjectApplication frm = new frmMaintainProjectApplication();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedBptApplication = mSelectdeApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectdeApplication == null)
            {
                MessageBox.Show("请选择一个制图申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中制图申请？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectdeApplication.Status = "DRAW06";
                mSelectdeApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectdeApplication.LastModifiedDate = DateTime.Now;
                bool successful = BptApplicationBs.UpdateBptApplication(mSelectdeApplication);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtProApplicationCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationQuery frm = new frmMaintainProductPlanApplicationQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanApplication != null)
            {
                txtProApplicationCode.Text = frm.SelectedProdPlanApplication.ApplicationCode;
                txtProApplicationCode.Tag = frm.SelectedProdPlanApplication;
            }
            else
            {
                txtProApplicationCode.Text = "";
                txtProApplicationCode.Tag = null;
            }
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }
        #endregion
    }
}
